#include "widget.h"
#include "ui_widget.h"
#include <QUrl>
#include <QQmlEngine>
#include <QDesktopWidget>
#include <QQmlContext>
#include <QPalette>
#include <QPixmap>
#include <QBrush>
#include <QColor>
#include <QIcon>
#include <QMenu>
#include <QAction>
#include <QTimer>
#include <QSettings>
#include <QApplication>
#include <QDebug>
#include <QDir>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    setWindowFlags(this->windowFlags() | Qt::SubWindow | Qt::FramelessWindowHint);

    //背景透明
    setAttribute(Qt::WA_TranslucentBackground);

    //填充背景
    setAutoFillBackground(true);

    //画刷
    QPalette palette;
    palette.setColor(QPalette::Background, QColor(0x00, 0x00, 0x00, 0x00));
    setPalette(palette);

    //qml透明
    ui->quickWidget->setAttribute(Qt::WA_TranslucentBackground);
    ui->quickWidget->setClearColor(QColor(Qt::transparent));

    ui->quickWidget->engine()->rootContext()->setContextProperty("widget",this);
    ui->quickWidget->engine()->addImportPath(QDir::currentPath());
    ui->quickWidget->setSource(QUrl("qrc:/main.qml"));

    setSystemMenu();
    setGeometry(0,0,1,1);

    QTimer::singleShot(1000, this, &Widget::init);
}

Widget::~Widget()
{
    delete ui->quickWidget;
    delete ui;
}
//添加系统托盘
void Widget::setSystemMenu()
{
    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(QIcon(":/favicon.ico"));

    QMenu *menu = new QMenu(this);
    QAction *action1 = menu->addAction("显示");
    connect(action1, &QAction::triggered, this, &Widget::showAction);

    QAction *action2 = menu->addAction("隐藏");
    connect(action2, &QAction::triggered, this, &Widget::hideAction);

    menu->addSeparator();

    QAction *action = menu->addAction("退出");
    connect(action, &QAction::triggered, this, &Widget::systemExit);

    trayIcon->setContextMenu(menu);
    trayIcon->show();
}

void Widget::systemExit(bool b)
{
    (void)b;
    qApp->quit();
}

void Widget::init()
{
    setWindowState(Qt::WindowMaximized);
    setGeometry(0,0,QApplication::desktop()->width(), 352*3);
}

void Widget::showAction(bool b)
{
    (void)b;
    show();
}

void Widget::hideAction(bool b)
{
    (void)b;
    hide();
}
